<?php

class ServicesController extends Core_Controller_Action 
{	
	public $menuType = 6;
	public $_news;
	public $newsLimit = 12;
	
	public function init() {
		$this->view->menuType = $this->menuType;
		$this->_news = new News();
	}
	
	public function indexAction() {

		$page = intval( $this->_request->getParam( 'page',1 ) );
		
		$pager = $this->_news->getListWithPagerService(6,$page,12, $this->view->baseUrl(). '/services/index/page/{%page_number}/type/' );
		if( $pager ) {
				$pagerLayout = $pager['layout'];
				$pagerLayout->setTemplate('<a class="nums" href="{%url}">{%page}</a>');
				$pagerLayout->setSelectedTemplate('<a class="nums focus" href="{%url}">{%page}</a>');
				
				$pager = $pager['pager'];
		
				$this->view->list = $pagerLayout->execute();
				$this->view->pager = $pagerLayout;
				
				$this->view->result = $pager->getNumResults();
				$this->view->page = $page;
				$this->view->pathInfo = $this->view->baseUrl() . $this->_request->getPathInfo();
				$this->view->limit = $this->newsLimit;
			
			} else {
				$this->view->emptyResult = true;
			}
		
	}
	
	
	public function showAction() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		$parent = intval( $this->_request->getParam( 'p', 0 ) );
		
		$pager = null;
		$tmp = null;
		
		if( $id > 0 ) {			
			$this->view->id = $id;
			$this->view->parent = $parent;
	
			$cat_type = new Categries();
			$type = $cat_type->getItem($id)->getFirst();
			
			
			
			if($type['type'] == 1)
			{
			$pager = $this->_news->getListByCategoryWithPagerService( $id, $this->view->baseUrl(). '/services/show/page/{%page_number}/id/'.$id.'/p/'.$parent, $page, $this->newsLimit );	
			$this->view->type = $type['type'];
			}	
			
			else
			{	
			$pager = $this->_news->getListByCategoryWithPagerService( $id, "", 1, 1);
			$this->view->type = 2;
			if($pager)
			{
			$tmp = $pager['layout']->execute()->getFirst();
			//$this->_redirect( 'services/article/id/'.$tmp['news_id'] ); 
			}
			
			}
			/*else
			
			}*/
			
				
			if( $pager ) {
				
		
				if($type['type'] == 1)
				{
				$pagerLayout = $pager['layout'];
				$pagerLayout->setTemplate('<a class="nums" href="{%url}">{%page}</a>');
				$pagerLayout->setSelectedTemplate('<a class="nums focus" href="{%url}">{%page}</a>');
				
				$pager = $pager['pager'];
				$this->view->list = $pagerLayout->execute();
				$this->view->pager = $pagerLayout;
				
				$this->view->result = $pager->getNumResults();
				$this->view->page = $page;
				$this->view->pathInfo = $this->view->baseUrl() . $this->_request->getPathInfo();
				$this->view->limit = $this->newsLimit;
				}
				
				else
				{
				
				try{
				$list = $this->_news->getItemService( $tmp['news_id'] );	
				$this->view->list = $list->getFirst()->toArray();
				}
				catch(Exception $e){	
				
				$this->view->type = 1;
				$this->view->emptyResult = true;
				}
				}
				
			
			} else {
				//$this->view->type = 1;
				$this->view->emptyResult = true;
			}
			
		} else {
			$this->_forward( 'p404', 'index' );
		}
	}
	
	public function articleAction() {
		$id = intval( $this->_request->getParam( 'id', false ) );

		if( $id ) {
			$item = $this->_news->getItemService( $id );
			if( $item ) {		
				//print_r($item);		
				$this->view->item = $item->getFirst()->toArray();
			} else {
				$this->_forward( 'p404', 'index' );
			}
		} else {
			$this->view->emptyResult = true;
			//$this->_forward( 'p404', 'index' );
		}
	}

}